package com.chixing.day17.ThreadTest;

public class Account {
    private String accName;
    private double accPrice;

    public Account() {
    }

    public Account(String accName, double accPrice) {
        this.accName = accName;
        this.accPrice = accPrice;
    }

    public String getAccName() {
        return accName;
    }

    public void setAccName(String accName) {
        this.accName = accName;
    }

    public double getAccPrice() {
        return accPrice;
    }

    public void setAccPrice(double accPrice) {
        this.accPrice = accPrice;
    }
    //synchronized:当前线程，拥有该account对象的锁
    public synchronized double depoist(double money){
        //模拟实际生活中 存款消耗的时间
        double temp = this.accPrice;
        temp += money;
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        this.accPrice = temp;
        System.out.println( Thread.currentThread().getName() + " temp当前账户余额：" + this.accPrice);
        return this.accPrice;
        }

    public synchronized double withdraw(double money) {
        double temp = this.accPrice;
        temp -= money;
        try {
            Thread.sleep(2);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        this.accPrice = temp;
        System.out.println( Thread.currentThread().getName() + " temp当前账户所剩余额：" + this.accPrice);
        return this.accPrice;
    }
}
